﻿Imports System.Data.OleDb
Imports System.IO

Public Class frmDetailsVehicule
    Dim vehicule As clsVehicule



    Dim bdVehicule As New cls_GestionBD("taco.mdb")
    Dim bdCategories As New cls_GestionBD("taco.mdb")

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        btnAppliquer.PerformClick()

        'If noVehiculeModifier <> Nothing Then
        '    With bdVehicule.dsBd.Tables("tblVehicule").Rows(0)
        '        .Item("noImmatriculation") = txtNoImmatriculation.Text
        '        .Item("actif") = chkVehiculeActif.Checked
        '        .Item("marque") = cboMarque.Text
        '        .Item("modele") = cboModele.Text
        '        .Item("annee") = txtAnnee.Text
        '        .Item("valeur") = updValeur.Value
        '        .Item("kilometrage") = txtKilometrage.Text
        '        .Item("nbrPlaces") = updNbrPlaces.Value
        '        .Item("capaciteChargement") = txtCapaciteChargement.Text
        '        .Item("transmission") = cboTransmission.Text
        '        .Item("commentaire") = txtCommentaires.Text
        '    End With

        '    bdVehicule.miseAJourBD("tblVehicule")
        'Else
        '    creerVehicule()
        'End If

        Me.Close()
        frmGestionVehicules.btnChercher.PerformClick()
    End Sub

    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        Me.Close()
    End Sub

    Private Sub frmDetailsVehicule_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'If noVehiculeModifier <> Nothing Then
        '    bdVehicule.daBd.SelectCommand = New OleDbCommand("SELECT * FROM tblVehicules WHERE noVehicule = @noVehicule")
        '    bdVehicule.daBd.SelectCommand.Parameters.Add("@noVehicule", OleDbType.Integer).Value = noVehiculeModifier

        '    bdVehicule.remplirDataSet("tblVehicule")

        '    With bdVehicule.dsBd.Tables("tblVehicule").Rows(0)
        '        txtNoImmatriculation.Text = .Item("noImmatriculation")
        '        chkVehiculeActif.Checked = nvl(.Item("actif"), True)
        '        cboMarque.Text = nvl(.Item("marque"), "")
        '        cboModele.Text = nvl(.Item("modele"), "")
        '        txtAnnee.Text = nvl(.Item("annee"), "")
        '        updValeur.Value = nvl(.Item("valeur"), 0)
        '        txtKilometrage.Text = nvl(.Item("kilometrage"), 0)
        '        updNbrPlaces.Value = nvl(.Item("nbrPlaces"), 0)
        '        txtCapaciteChargement.Text = nvl(.Item("capaciteChargement"), 0)
        '        cboTransmission.Text = nvl(.Item("transmission"), "")
        '        txtCommentaires.Text = nvl(.Item("commentaire"), "")
        '    End With


        '    bdCategories.daBd.SelectCommand = New OleDbCommand("SELECT nomCategorie FROM tblCategories WHERE noCategorie = @noCategorie")
        '    bdCategories.daBd.SelectCommand.Parameters.Add("@noCategorie", OleDbType.VarChar, 15).Value = bdVehicule.dsBd.Tables("tblVehicule").Rows(0).Item("noCategorie")
        '    bdCategories.remplirDataSet("tblCategorie")

        '    remplirOptions()
        'End If

        'remplirCategories()
        'remplirMarques()

        'If noVehiculeModifier <> Nothing Then
        '    'marche si après
        '    cboCategories.Text = nvl(bdCategories.dsBd.Tables("tblCategorie").Rows(0).Item("nomCategorie"), "")
        'End If

    End Sub

    'permet de remplir le comboBox des catégories de toutes les catégories disponibles
    'Sub remplirCategories()

    '    Dim bd As New cls_GestionBD("taco.mdb")
    '    bd.daBd.SelectCommand = New OleDbCommand("SELECT DISTINCT nomCategorie FROM tblCategories")
    '    bd.remplirDataSet("tblCategories")

    '    cboCategories.Items.Clear()

    '    For Each element As DataRow In bd.dsBd.Tables("tblCategories").Rows
    '        cboCategories.Items.Add(element.Item(0))
    '    Next

    'End Sub

    'permet de remplir le comboBox des marques de toutes les marques disponibles
    Sub remplirMarques()

        Dim bd As New cls_GestionBD("taco.mdb")
        bd.daBd.SelectCommand = New OleDbCommand("SELECT DISTINCT marque FROM tblVehicules")
        bd.remplirDataSet("tblMarques")

        cboMarque.Items.Clear()

        For Each element As DataRow In bd.dsBd.Tables("tblMarques").Rows
            cboMarque.Items.Add(element.Item(0))
        Next

    End Sub

    'permet de remplir le comboBox des modèles de tous les modèles disponibles pour la marque sélectionnée
    Sub remplirModeles() Handles cboMarque.TextChanged

        Dim bd As New cls_GestionBD("taco.mdb")
        bd.daBd.SelectCommand = New OleDbCommand("SELECT DISTINCT modele FROM tblVehicules WHERE marque = @marque")
        bd.daBd.SelectCommand.Parameters.Add("@marque", OleDbType.VarChar).Value = cboMarque.Text
        bd.remplirDataSet("tblModeles")

        cboModele.Items.Clear()

        For Each element As DataRow In bd.dsBd.Tables("tblModeles").Rows
            cboModele.Items.Add(element.Item(0))
        Next

    End Sub

    'permet de remplir la liste des options de toutes les options disponibles
    'Sub remplirOptions()
    '    Dim bd As New cls_GestionBD("taco.mdb")
    '    bd.daBd.SelectCommand = New OleDbCommand("SELECT nomOption FROM tblOptions WHERE noOption IN( " & _
    '                                             "SELECT noOption FROM tblOptionsVehicules WHERE noVehicule IN( " & _
    '                                             "SELECT noVehicule FROM tblVehicules WHERE noVehicule = @noVehicule ))")
    '    bd.daBd.SelectCommand.Parameters.Add("@noVehicule", OleDbType.VarChar).Value = noVehiculeModifier
    '    bd.remplirDataSet("tblOptions")

    '    lstOptions.Items.Clear()

    '    For Each element As DataRow In bd.dsBd.Tables("tblOptions").Rows
    '        lstOptions.Items.Add(element.Item(0))
    '    Next

    'End Sub

    'Sub creerVehicule()

    '    Dim bdCreerVehicule As New cls_GestionBD("taco.mdb")
    '    bdCreerVehicule.daBd.SelectCommand = New OleDbCommand("SELECT * FROM tblVehicules")
    '    bdCreerVehicule.remplirDataSet("tblVehicule")

    '    Dim bdCategorie As New cls_GestionBD("taco.mdb")
    '    bdCategorie.daBd.SelectCommand = New OleDbCommand("SELECT noCategorie FROM tblCategories WHERE nomCategorie = @nomCategorie")
    '    bdCategorie.daBd.SelectCommand.Parameters.Add("@nomCategorie", OleDbType.VarChar, 50).Value = cboCategories.Text
    '    bdCategorie.remplirDataSet("tblCategorie")

    '    Dim lNewRow As DataRow
    '    lNewRow = bdCreerVehicule.dsBd.Tables("tblVehicule").NewRow

    '    With lNewRow
    '        .Item("noVehicule") = createNewNo("noVehicule", "tblVehicules")
    '        .Item("noImmatriculation") = nvl(txtNoImmatriculation.Text, DBNull.Value)
    '        .Item("noCategorie") = nvl(bdCategorie.dsBd.Tables("tblCategorie").Rows(0).Item("noCategorie"), DBNull.Value)
    '        .Item("marque") = nvl(cboMarque.Text, DBNull.Value)
    '        .Item("modele") = nvl(cboModele.Text, DBNull.Value)
    '        .Item("annee") = nvl(txtAnnee.Text, DBNull.Value)
    '        .Item("valeur") = nvl(updValeur.Value, DBNull.Value)
    '        .Item("kilometrage") = nvl(txtKilometrage.Text, DBNull.Value)
    '        .Item("nbrPlaces") = nvl(updNbrPlaces.Value, DBNull.Value)
    '        .Item("capaciteChargement") = nvl(txtCapaciteChargement.Text, DBNull.Value)
    '        .Item("transmission") = nvl(cboTransmission.Text, DBNull.Value)
    '        .Item("actif") = True
    '    End With

    '    bdCreerVehicule.dsBd.Tables("tblVehicule").Rows.Add(lNewRow)

    '    bdCreerVehicule.miseAJourBD("tblVehicule")
    '    noVehiculeModifier = Nothing

    'End Sub

    Private Sub frmDetailsVehicule_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Me.Dispose()
    End Sub

    Public Overloads Function ShowDialog(ByRef ve As clsVehicule) As Windows.Forms.DialogResult

        vehicule = ve

        'affiche les options
        lstOptions.Items.Clear()
        For Each opt As clsOptions In vehicule.options
            lstOptions.Items.Add(opt.nom)
        Next

        'remplit le comboBox des catégories
        cboCategories.Items.Clear()
        For Each categorie As clsCategorie In categories
            cboCategories.Items.Add(categorie.nomCategorie)
        Next

        'remplit le comboBox des marques
        remplirMarques()

        With vehicule
            txtNoImmatriculation.Text = .noImmatriculation
            chkVehiculeActif.Checked = .actif
            cboCategories.Text = .categorie.nomCategorie
            cboMarque.Text = .marque
            cboModele.Text = .modele
            txtAnnee.Text = .annee
            updValeur.Value = .valeur
            txtKilometrage.Text = .kilometrage
            updNbrPlaces.Value = .nbrPlaces
            txtCapaciteChargement.Text = .capaciteChargement
            cboTransmission.Text = .transmission
            txtCommentaires.Text = .commentaire
        End With

        If cboCategories.Text = Nothing Then
            cboCategories.Text = cboCategories.Items(0)
        End If

        btnAppliquer.Enabled = False

        'load up order defaults
        Return MyBase.ShowDialog()
    End Function

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        frmChoisirOptions.ShowDialog()
    End Sub

    Private Sub btnEnlever_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnlever.Click
        If lstOptions.SelectedItems.Count = 1 Then
            lstOptions.Items.Remove(lstOptions.SelectedItem)
        End If
    End Sub

    Private Sub txtNoImmatriculation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    txtNoImmatriculation.TextChanged, cboCategories.TextChanged, cboMarque.TextChanged, cboModele.TextChanged, txtAnnee.TextChanged, _
    updValeur.ValueChanged, txtKilometrage.TextChanged, updNbrPlaces.ValueChanged, txtCapaciteChargement.TextChanged, _
    cboTransmission.TextChanged, txtCommentaires.TextChanged

        btnAppliquer.Enabled = True
    End Sub

    Private Sub btnAppliquer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppliquer.Click

        With vehicule
            .noImmatriculation = txtNoImmatriculation.Text
            .actif = chkVehiculeActif.Checked
            .marque = cboMarque.Text
            .modele = cboModele.Text
            .annee = txtAnnee.Text
            .valeur = updValeur.Value
            .kilometrage = txtKilometrage.Text
            .nbrPlaces = updNbrPlaces.Value
            .capaciteChargement = txtCapaciteChargement.Text
            .transmission = cboTransmission.Text
            .commentaire = txtCommentaires.Text
        End With

        For Each categorie As clsCategorie In categories
            If categorie.nomCategorie = cboCategories.Text Then
                vehicule.categorie = categorie
                Exit For
            End If
        Next

        vehicule.options.Clear()

        For Each opt As clsOptions In options
            If lstOptions.Items.Contains(opt.nom) Then
                vehicule.options.Add(opt)
            End If
        Next

        vehicule.enregistrer()

        btnAppliquer.Enabled = False
    End Sub

    Private Sub btnChercher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChercher.Click
        OpenFileDialog1.ShowDialog()

        MsgBox(OpenFileDialog1.FileName)

    End Sub
End Class